package com.kust.smarthome.web.controller;

import com.kust.smarthome.web.dto.DeviceTableDto;
import com.kust.smarthome.web.model.*;
import com.kust.smarthome.web.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/deviceState")
@Api(value = "设备状态表相关操作", tags = {"device-tag"})
public class DeviceTableController {

    @Resource
    private IAirConditionerService airConditionerService;

    @Resource
    private ILampService lampService;

    @Resource
    private ITelevisionService televisionService;

    @Resource
    private ICurtainService curtainService;

    @Resource
    private IHygrothermographService hygrothermographService;

    @Resource
    private IDetectorService detectorService;

    @Resource
    private IDoorService doorService;

    @Resource
    IUserService userService;

    @GetMapping("/everydevice")
    @ApiOperation(value = "获取收所有设备")
    public List<DeviceTableDto> getEverydevice(Principal principal){
        User user = userService.getByUsername(principal.getName());

        List<DeviceTableDto> list = new ArrayList<>();

        //灯--1
        List<Lamp> lamps = lampService.getByUserId(user.getId());
        for (Lamp lamp : lamps) {
            DeviceTableDto deviceTableDto = new DeviceTableDto();
            deviceTableDto
                    .setTypeId(1)
                    .setDeviceId(lamp.getId())
                    .setName(lamp.getName())
                    .setContent(lamp.getContent())
                    .setState(lamp.getState())
                    .setRoomId(lamp.getRoomId());
            list.add(deviceTableDto);
        }

        //窗帘--2
        List<Curtain> curtains = curtainService.getByUserId(user.getId());
        for (Curtain curtain : curtains) {
            DeviceTableDto deviceTableDto = new DeviceTableDto();
            deviceTableDto
                    .setTypeId(2)
                    .setName(curtain.getName())
                    .setDeviceId(curtain.getId())

                    .setContent(curtain.getContent())
                    .setState(curtain.getState())
                    .setRoomId(curtain.getRoomId());
            list.add(deviceTableDto);
        }

        //门--3
        List<Door> doors = doorService.getByUserId(user.getId());
        for (Door door : doors) {
            DeviceTableDto deviceTableDto = new DeviceTableDto();
            deviceTableDto
                    .setTypeId(3)
                    .setName(door.getName())
                    .setDeviceId(door.getId())
                    .setContent(door.getContent())
                    .setState(door.getState())
                    .setRoomId(door.getRoomId());
            list.add(deviceTableDto);
        }

        //烟雾报警器--4
        List<Detector> detectors = detectorService.getByUserId(user.getId());
        for (Detector detector : detectors) {
            DeviceTableDto deviceTableDto = new DeviceTableDto();
            deviceTableDto
                    .setTypeId(4)
                    .setName(detector.getName())
                    .setDeviceId(detector.getId())
                    .setContent(detector.getContent())
                    .setState(detector.getState())
                    .setRoomId(detector.getRoomId());
            list.add(deviceTableDto);
        }

        //电视--5
        List<Television> televisions = televisionService.getByUserId(user.getId());
        for (Television television : televisions) {
            DeviceTableDto deviceTableDto = new DeviceTableDto();
            deviceTableDto
                    .setTypeId(5)
                    .setName(television.getName())
                    .setDeviceId(television.getId())
                    .setContent(television.getContent())
                    .setState(television.getState())
                    .setRoomId(television.getRoomId());
            list.add(deviceTableDto);
        }

        //空调--6
        List<AirConditioner> airConditioners = airConditionerService.getByUserId(user.getId());
        for (AirConditioner airConditioner : airConditioners) {
            DeviceTableDto deviceTableDto = new DeviceTableDto();
            deviceTableDto
                    .setTypeId(6)
                    .setName(airConditioner.getName())
                    .setDeviceId(airConditioner.getId())
                    .setContent(airConditioner.getContent())
                    .setState(airConditioner.getState())
                    .setRoomId(airConditioner.getRoomId());
            list.add(deviceTableDto);
        }

        //温度--7
        List<Hygrothermograph> hygrothermographs = hygrothermographService.getByUserId(user.getId());
        for (Hygrothermograph hygrothermograph : hygrothermographs) {
            DeviceTableDto deviceTableDto = new DeviceTableDto();
            deviceTableDto
                    .setTypeId(7)
                    .setName(hygrothermograph.getName())
                    .setDeviceId(hygrothermograph.getId())
                    .setContent(hygrothermograph.getContent())
                    .setState(hygrothermograph.getState())
                    .setRoomId(hygrothermograph.getRoomId());
            list.add(deviceTableDto);
        }
        return list;
    }

    @GetMapping("/every")
    @ApiOperation(value = "获取收所有设备")
    public List<DeviceTableDto> getEvery(Principal principal){
        User user = userService.getByUsername(principal.getName());

        List<DeviceTableDto> list = new ArrayList<>();

        //灯--1
        List<Lamp> lamps = lampService.getByUserId(user.getId());
        List<DeviceTableDto> list2 = new ArrayList<>();
        DeviceTableDto deviceTableDto3 = new DeviceTableDto();
        for (Lamp lamp : lamps) {
            DeviceTableDto deviceTableDto2 = new DeviceTableDto();
            deviceTableDto2
                    .setDeviceId(lamp.getId())
                    .setRoomId(lamp.getRoomId());
            list2.add(deviceTableDto2);
        }

        //窗帘--2
        List<Curtain> curtains = curtainService.getByUserId(user.getId());
        List<DeviceTableDto> list4 = new ArrayList<>();
        DeviceTableDto deviceTableDto7 = new DeviceTableDto();
        for (Curtain curtain : curtains) {
            DeviceTableDto deviceTableDto6 = new DeviceTableDto();
            deviceTableDto6
                    .setDeviceId(curtain.getId())
                    .setRoomId(curtain.getRoomId());
            list4.add(deviceTableDto6);
        }

        //门--3
        List<Door> doors = doorService.getByUserId(user.getId());
        List<DeviceTableDto> list7 = new ArrayList<>();
        DeviceTableDto deviceTableDto13 = new DeviceTableDto();
        for (Door door : doors) {
            DeviceTableDto deviceTableDto12 = new DeviceTableDto();
            deviceTableDto12
                    .setDeviceId(door.getId())
                    .setRoomId(door.getRoomId());
            list7.add(deviceTableDto12);
        }

        //烟雾报警器--4
        List<Detector> detectors = detectorService.getByUserId(user.getId());
        List<DeviceTableDto> list6 = new ArrayList<>();
        DeviceTableDto deviceTableDto11 = new DeviceTableDto();
        for (Detector detector : detectors) {
            DeviceTableDto deviceTableDto10 = new DeviceTableDto();
            deviceTableDto10
                    .setDeviceId(detector.getId())
                    .setRoomId(detector.getRoomId());
            list6.add(deviceTableDto10);
        }

        //电视--5
        List<Television> televisions = televisionService.getByUserId(user.getId());
        List<DeviceTableDto> list3 = new ArrayList<>();
        DeviceTableDto deviceTableDto5 = new DeviceTableDto();
        for (Television television : televisions) {
            DeviceTableDto deviceTableDto4 = new DeviceTableDto();
            deviceTableDto4
                    .setDeviceId(television.getId())
                    .setRoomId(television.getRoomId());
            list3.add(deviceTableDto4);
        }

        //空调--6
        List<AirConditioner> airConditioners = airConditionerService.getByUserId(user.getId());
        List<DeviceTableDto> list1 = new ArrayList<>();
        DeviceTableDto deviceTableDto1 = new DeviceTableDto();
        for (AirConditioner airConditioner : airConditioners) {
            DeviceTableDto deviceTableDto = new DeviceTableDto();
            deviceTableDto
                    .setDeviceId(airConditioner.getId())
                    .setRoomId(airConditioner.getRoomId());
            list1.add(deviceTableDto);
        }

        //温度--7
        List<Hygrothermograph> hygrothermographs = hygrothermographService.getByUserId(user.getId());
        List<DeviceTableDto> list5 = new ArrayList<>();
        DeviceTableDto deviceTableDto9 = new DeviceTableDto();
        for (Hygrothermograph hygrothermograph : hygrothermographs) {
            DeviceTableDto deviceTableDto8 = new DeviceTableDto();
            deviceTableDto8
                    .setDeviceId(hygrothermograph.getId())
                    .setRoomId(hygrothermograph.getRoomId());
            list5.add(deviceTableDto8);
        }

        return list;
    }

}
